﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using KDS.Mes.Mes.TestSlides.Dtos;
using KDS.Mes.Dto;
using KDS.Mes.DataExporting.Excel.EpPlus;
using Abp.Timing.Timezone;
using Abp.Runtime.Session;

namespace KDS.Mes.Mes.TestSlides
{
    /// <summary>
    /// 滑道检测数据表的导出EXCEL功能的实现
    /// </summary>
    public class TestSlideListExcelExporter : EpPlusExcelExporterBase, ITestSlideListExcelExporter
    {

        private readonly ITimeZoneConverter _timeZoneConverter;
        private readonly IAbpSession _abpSession;


        /// <summary>
        /// 构造方法
        /// </summary>
        public TestSlideListExcelExporter(ITimeZoneConverter timeZoneConverter, IAbpSession abpSession)
        {
            _timeZoneConverter = timeZoneConverter;
            _abpSession = abpSession;
        }



        /// <summary>
        /// 导出滑道检测数据表到EXCEL文件
        /// <param name="testSlideListDtos">导出信息的DTO</param>
        /// </summary>
        public FileDto ExportTestSlideToFile(List<TestSlideListDto> testSlideListDtos)
        {


            var file = CreateExcelPackage("testSlideList.xlsx", excelPackage =>
            {

                var sheet = excelPackage.Workbook.Worksheets.Add(L("TestSlide"));
                sheet.OutLineApplyStyle = true;

                AddHeader(
                    sheet,
                      L("LINE"), L("STATION_NO"), L("SEAT_ID"), L("RESULT"), L("SLIDE_FWD_MAX"), L("SLIDE_FWD_MIN"),
                        L("SLIDE_FWD_AVR"), L("SLIDE_FWD_JUG"),L("SLIDE_BWD_MAX"), L("SLIDE_BWD_MIN"), 
                        L("SLIDE_BWD_AVR"),L("SLIDE_BWD_JUG"), L("LEVER_MAX"),L("LEVER_MIN"),L("LEVER_AVR"),
                        L("LEVER_JUG"),L("CREAT_TIME")
                    );
                AddObjects(sheet, 2, testSlideListDtos,

             _ => _.LINE,

             _ => _.STATION_NO,

             _ => _.SEAT_ID,

             _ => _.RESULT,

             _ => _.SLIDE_FWD_MAX,

             _ => _.SLIDE_FWD_MIN,

             _ => _.SLIDE_FWD_AVR,

             _ => _.SLIDE_FWD_JUG,

             _ => _.SLIDE_BWD_MAX,

             _ => _.SLIDE_BWD_MIN,

             _ => _.SLIDE_BWD_AVR,

             _ => _.SLIDE_BWD_JUG,

             _ => _.LEVER_MAX,

             _ => _.LEVER_MIN,

             _ => _.LEVER_AVR,

             _ => _.LEVER_JUG,

        _ => _timeZoneConverter.Convert(_.CREAT_TIME, _abpSession.TenantId, _abpSession.GetUserId())
       );
                //写个时间转换的吧
                //var creationTimeColumn = sheet.Column(10);
                //creationTimeColumn.Style.Numberformat.Format = "yyyy-mm-dd";

                for (var i = 1; i <= 17; i++)
                {
                    sheet.Column(i).AutoFit();
                }

            });
            return file;

        }







    }
}
